<template>
<div class="goods">
  <headOfShop class=""></headOfShop>
  <!-- 店铺head-->
  <section class="shop-head">
    <div class="shop-img"></div>
    <div class="shop-title">
      <p>三元桥自主售货机
        <img src="../../assets/piao.png" class="shop-icon" alt="">
      </p>
      <p class="shop-activity">
        活动:1月23日-1月24日,每单满50元返现,每单满50元返现每单满50元返现每单满50元返现
      </p>
      <p class="shop-address">
        北京市朝阳区三元桥2345号静安中心一楼34-6号店旁
      </p>
    </div>
  </section>
  <section class="shop-content">
    <div class="menu-wrapper" ref="menuWrapper">
      <ul>
        <li v-for="(item,index) in goods" class="menu-item" :key="index" :class="{'current':currentIndex===index}" @click="selectMenu(index,$event)" ref="menuList">
          <div class="right-line" v-show="currentIndex===index"></div>
          <span class="text">
            <span v-show="item.type>0" class="icon" :class="classMap[item.type]"></span>{{item.name}}
          </span>
        </li>
      </ul>
    </div>

    <div class="foods-wrapper" ref="foodsWrapper">
      <ul>
        <li v-for="(item, index) in goods" :key="index" class="food-list" ref="foodList">
          <h1 class="title">{{item.name}}</h1>
          <ul>
            <li @click="selectFood(food,$event)" v-for="(food, index) in item.foods" :key="index" class="food-item">
              <div class="icon">
                <img :src="food.icon">
              </div>
              <div class="content">
                <h2 class="name">{{food.name}}</h2>
                <p class="desc">{{food.info}}</p>
                <div class="price">
                  <span class="now">￥{{food.price}}</span> <span class="unit"> 元/盒</span>
                </div>
                <div class="cartcontrol-wrapper">
                  <cartcontrol @add="addFood" :food="food"></cartcontrol>
                </div>
              </div>
              <div class="line"></div>
            </li>
          </ul>
        </li>
      </ul>
    </div>
  </section>
  <section class="shop-footer">
    <shopcart ref="shopcart" :selectFoods="selectFoods"></shopcart>
  </section>

</div>
</template>
<script>
import BScroll from 'better-scroll'
import data from './data.json'
import cartcontrol from './components/cartcontrol.vue'
import shopcart from './components/shopcart.vue'
import headOfShop from '../../components/HeadOfShop.vue'
export default {
  data () {
    return {
      goods: data.data,
      listHeight: [],
      scrollY: 0,
      selectedFood: {}
    }
  },
  props: {
    seller: {
      type: Object
    }
  },
  computed: {
    currentIndex () {
      for (let i = 0; i < this.listHeight.length; i++) {
        let height1 = this.listHeight[i]
        let height2 = this.listHeight[i + 1]
        if (!height2 || (this.scrollY >= height1 && this.scrollY < height2)) {
          this._followScroll(i)
          return i
        }
      }
      return 0
    },
    selectFoods () {
      let foods = []
      this.goods.forEach((good) => {
        good.foods.forEach((food) => {
          if (food.count) {
            foods.push(food)
          }
        })
      })
      return foods
    }
  },
  created () {
    this.classMap = ['decrease', 'discount', 'special', 'invoice', 'guarantee']
    this.$nextTick(() => {
      this._initScroll()
      this._calculateHeight()
    })
  },
  components: {
    cartcontrol,
    shopcart,
    headOfShop
  },
  methods: {
    selectMenu (index, event) {
      if (!event._constructed) {
        return
      }
      let foodList = this.$refs.foodList
      let el = foodList[index]
      this.foodsScroll.scrollToElement(el, 300)
    },
    selectFood (food, event) {
      if (!event._constructed) {
        return
      }
      this.selectedFood = food
      // this.$refs.food.show()
    },
    addFood (target) {
      this._drop(target)
    },
    _drop (target) {
      // 体验优化,异步执行下落动画
      this.$nextTick(() => {
        this.$refs.shopcart.drop(target)
      })
    },
    _initScroll () {
      this.meunScroll = new BScroll(this.$refs.menuWrapper, {
        click: true
      })

      this.foodsScroll = new BScroll(this.$refs.foodsWrapper, {
        click: true,
        probeType: 3
      })

      this.foodsScroll.on('scroll', (pos) => {
        // 判断滑动方向，避免下拉时分类高亮错误（如第一分类商品数量为1时，下拉使得第二分类高亮）
        if (pos.y <= 0) {
          this.scrollY = Math.abs(Math.round(pos.y))
        }
      })
    },
    _calculateHeight () {
      let foodList = this.$refs.foodList
      let height = 0
      this.listHeight.push(height)
      for (let i = 0; i < foodList.length; i++) {
        let item = foodList[i]
        height += item.clientHeight
        this.listHeight.push(height)
      }
    },
    _followScroll (index) {
      let menuList = this.$refs.menuList
      let el = menuList[index]
      this.meunScroll.scrollToElement(el, 300, 0, -100)
    }
  }
}
</script>

<style lang="scss" scoped>
.goods {
    position: absolute;
    top: 0;
    bottom: 1.33rem;
    width: 100%;
    overflow: hidden;
}
.shop-head {
    width: 100%;
    height: 2.53rem;
    // background-color: red;
    display: flex;
    .shop-img {
        width: 2.27rem;
        height: 2rem;
        margin-left: 0.4rem;
        margin-right: 0.27rem;
        background-size: 100% 100%;
        background-image: url("../../assets/shop.png");
    }
    .shop-title {
        height: 2rem;
        padding-top: 0.13rem;
        margin-right: 0.3rem;
        p {
            font-size: 0.48rem;
            padding: 0.07rem 0;
        }
        .shop-icon {
            width: 0.47rem;
            height: 0.47rem;
            float: right;
            margin-right: 0.27rem;
        }
        .shop-activity {
            width: 6.27rem;
            height: 0.32rem;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            font-size: 0.32rem;
            color: red;
        }
        .shop-address {
            // width: 5.6rem;
            font-size: 0.32rem;
            line-height: 0.43rem;
        }
    }
}
.shop-content {
    width: 100%;
    height: 20rem;
    overflow: hidden;
    // background-color: red;
    display: flex;
    .menu-wrapper {
        width: 2.4rem;
        flex: 0 0 2.4rem;
        background: #f3f5f7;
        .menu-item {
            display: flex;
            align-items: center;
            justify-content: center;
            line-height: 14px;
            width: 2.37rem;
            height: 1.2rem;
            background-color: #ffffff;
            font-size: 0.35rem;
            color: #333333;
            text-align: center;
            position: relative;
            &.current {
                position: relative;
                z-index: 10;
                margin-top: -1px;
                background: #fff;
                background-color: #f7f7f7;
                font-weight: 700;
            }
            .right-line {
                width: 0.05rem;
                height: 1.2rem;
                position: absolute;
                left: 0;
                background-color: #61a843;
            }
            // .icon {
            //     display: inline-block;
            //     vertical-align: top;
            //     width: 12px;
            //     height: 12px;
            //     margin-right: 2px;
            //     background-size: 12px 12px;
            //     background-repeat: no-repeat;
            // }
        }
    }
    .foods-wrapper {
        flex: 1;
        margin-left: 0.07rem;
        .title {
            padding-left: 14px;
            height: 0.55rem;
            line-height: 0.55rem;
            // border-left: 2px solid #d9dde1;
            font-size: 0.35rem;
            color: #5c5c5c;
            background: #f7f7f7;
        }
        .food-item {
            display: flex;
            padding-left: 0.37rem;
            padding-bottom: .253333rem;
            margin-top: 0.31rem;
            position: relative;
            // border-bottom: 1px solid #efefef;
            // border-1px(rgba(7, 17, 27, 0.1));
            &:last-child {
                // border-none()
                margin-bottom: 0;
            }
            .icon {
                flex: 0 0 57px;
                margin-right: .37rem;
                img {
                    width: 2.04rem;
                    height: 2.04rem;
                }
            }
            .line {
                width: 7.12rem;
                height: 1px;
                position: absolute;
                bottom: 0;
                background-color: #efefef;
            }
            .content {
                flex: 1;
                .name {
                    margin: 0.03rem 0 0.11rem;
                    height: 0.35rem;
                    font-weight: normal;
                    line-height: 0.48rem;
                    font-size: 0.35rem;
                    color: #1c1c1c;
                }
                .desc {
                    height: .67rem;
                    line-height: .36rem;
                    font-size: .24rem;
                    color: #a0a0a0;
                    overflow: hidden;
                    margin: 0.28rem 0;
                }
                .extra {
                    .count {
                        margin-right: 12px;
                    }
                }
                .price {
                    margin-top: 0.25rem;
                    display: inline-block;
                    .now {
                        font-size: 0.43rem;
                        font-weight: 700;
                        color: #fa5741;
                    }
                    .unit {
                        font-size: 0.24rem;
                        color: #fa5741;
                    }
                }
                .cartcontrol-wrapper {
                    float: right;
                    display: block;
                    margin-right: 0.35rem;
                    margin-top: 0.35rem;
                    // position: absolute;
                    // right: .39rem;
                    // bottom: .27rem;
                }
            }
        }
    }
}
.shop-footer {
    position: relative;
    width: 100%;
    height: 1.33rem;
    // background-color:green;
}
</style>
